import socket, os
import hashlib
server = socket.socket()
server.bind(('localhost', 9999))

server.listen()

while True:
    conn, addr = server.accept()
    while True:
        data = conn.recv(1024)
        if not data:
            print('客户端已断开')
            break
        cmd, filename = data.decode().split()
        print(filename)
        if os.path.isfile(filename):
            f = open(filename, 'rb')
            m = hashlib.md5()
            file_size = os.stat(filename).st_size
            conn.send(str(file_size).encode())   #给客户端发送文件大小
            conn.recv(1024)
            for line in f:
                m.update(line)
                conn.send(line)
            print('file md5:', m.hexdigest())
            f.close()
            conn.send(m.hexdigest().encode())

server.close